from framework.base_page import BasePage
from selenium.webdriver.common.by import By
import time


class ComicReadPage(BasePage):

    # 从详情页点击付费章节，进入阅读页
    def click_fufei_chapter(self):
        """从详情页点击付费章节，进入阅读页"""
        self.scroll_window(location="top")  # 滚动页面到顶部
        page_last_20 = self.find_element(
            selector="xpath=>//span[@class='btn btn-page page-last active']", isshow_error=1)  # 当前显示的是最新20话
        if not page_last_20:
            page_last_20 = self.find_element(selector="xpath=>//span[@class='btn btn-page page-last']")  # 当前显示的不是最新20话
            self.element_click(element=page_last_20, log_msg="最新20话按钮")
        time.sleep(1)  # 获取已购章节的接口有时较慢，需等其加载出来后再去获取未购买的章节
        lock_chapters = self.find_elements(
            selector="css=>li[class='item lock'][style='display: list-item;']")  # 找到所有的未解锁的章节
        if lock_chapters:
            self.move_to_element(el=lock_chapters[0], location="foot")
            self.element_click(element=lock_chapters[0], log_msg="未解锁章节")
        else:
            raise Exception("最新二十话未找到未解锁的付费章节，请手动测试购买章节！")

    # 等待付费弹窗弹出
    def wait_fufei_window(self):
        return self.is_visible(locator=
                               (By.XPATH, "//div[@class='layui-layer layui-layer-page' and @id='layui-layer100001']"),
                               timeout=15)

    # 点击购买此章节
    def click_purchase_this_chapter(self):
        self.element_click(selector="xpath=>//div[@class='btn' and text()='购买此章节']")

    # 等待toast弹出
    def wait_success_toast(self, toast_num=1):
        """
        toast弹窗
        :param toast_num: 1--购买章节的toast，2--弹幕发送成功的toast
        :return:
        """
        if toast_num == 1:
            return self.toast_is_visible(locator=(By.XPATH, "//div[@class='layui-layer-content']"), text="购买章节成功！")
        else:
            return self.toast_is_visible(locator=(By.XPATH, "//div[@class='layui-layer-content']"), text="弹幕发送成功!")

    # 点击引导
    def click_guide(self):
        self.element_click(selector="xpath=>//div[@class='guide-btn guide-skip-btn']")  # 点击 朕了解，直接看漫画

    # 点击发表按钮
    def click_send(self):
        if self.is_visible(locator=(By.CSS_SELECTOR, ".egg-mascot")):  # 等待弹幕按钮显示出来
            danmu_el = self.find_element(selector="css=>.egg-mascot")  # 找到弹幕按钮
            self.mouse_stop_on_elment(element=danmu_el)  # 鼠标悬停在弹幕按钮上
            self.element_click(selector="css=>.bar-text")
        else:
            raise Exception("右下角未找到弹幕按钮")

    # 输入弹幕
    def input_danmu_text(self):
        text = "哈哈哈，好好看哦"
        status = self.is_visible(locator=(By.XPATH, "//div[@class='read-footer footer-show']"))  # 等待输入弹幕框显示出来
        if status:
            self.input_text(selector="id=>dmTextArea", text=text)  # 输入文字
        else:
            raise Exception("未等到输入弹幕框显示出来")

    # 点击发送弹幕
    def click_send_danmu(self):
        self.element_click(selector="xpath=>//*[@id='dmTexrSendBtn']")
